﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MN.Framework.Extensions;
using System.IO;
using System.Drawing;

namespace MN.Framework.UI.Win.Controls
{
    public class MnPictureBox : PictureBox, IMnControl
    {
        public object getValue()
        {
            return this.InitialImage.ToByteArray();
        }


        public void setValue(object value)
        {
           byte[] data = (byte[])value;
           if (data != null)
           {
               MemoryStream ms = new MemoryStream(data);
               var img = Image.FromStream(ms);
               var newWidth = this.Width;
               //var newHeight = img.Height / (img.Width > newWidth ? img.Width / newWidth : newWidth / img.Width);                           
               int newHeight;
               if (img.Width > newWidth)
                   newHeight = (int)(img.Height / ((double)img.Width / newWidth));
               else
                   newHeight = (int)(img.Height * ((double)newWidth / img.Width));
               this.InitialImage = img;
               this.Image = img.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
               this.Height = newHeight;
           }
        }
    }
}
